﻿/**********************************************************************************
 * Project Name :   
 * Remarks      :  
 * Class Name   :  UtilityCommonTest 
 * Version      :  v1.0.0.0 
 * Author       :  Joey Liang 
 * Description  : 
 * Created Time :  2013/10/20 20:59:24
 * Update Man   :  Joey Liang
 * Update Time  :  2013/10/20 20:59:24
************************************************************************/

using IdleTest.DemoBeTest;
using IdleTest.MSTest;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IdleTest.DemoToUse
{
    [TestClass()]
    public class UtilityCommonTest
    {
        /// <summary>
        ///ForeachHandle 的测试
        ///</summary>
        public void ForeachHandleTestHelper(dynamic actionTest, bool hasBreakBefore = false, bool hasBreakAfter = false)
        {
            bool notBreak = !hasBreakAfter && !hasBreakBefore;

            IEnumerable<int> array = new int[] { 1, 2, 3, 4, 5 };
            List<int> arrayActual = new List<int>();
            Action<int> action = p => arrayActual.Add(p);
            Func<int, bool> breakBeforeFunc = null; // TODO: 初始化为适当的值
            Func<int, bool> breakAfterFunc = null; // TODO: 初始化为适当的值
            //UtilityCommon.ForeachHandle<int>(array, action, breakBeforeFunc, breakAfterFunc);
            if (notBreak)
            {
                actionTest(array, action);
            }
            else
            {
                actionTest(array, action, breakBeforeFunc, breakAfterFunc);
            }

            AssertCommon.AreEqual(array, arrayActual);

            //*************************************************************************************

            arrayActual = new List<int>();
            AssertCommon.IsEmpty<IEnumerable<int>, List<int>, List<int>>(
                new IEnumerable<int>[] { null, new int[] { }, new List<int>() },
                new List<int>[] { arrayActual },
                true,
                (pIn1, pIn2) =>
                {
                    //UtilityCommon.ForeachHandle<int>(pIn1, p => pIn2.Add(p));
                    if (notBreak)
                        actionTest(pIn1, new Action<int>(p => pIn2.Add(p)));
                    else
                        actionTest(pIn1, new Action<int>(p => pIn2.Add(p)), breakBeforeFunc, breakAfterFunc);

                    return pIn2;
                });

            if (notBreak)
                return;

            //*************************************************************************************
            // If Has Break Function

            breakBeforeFunc = p => p > 4;
            breakAfterFunc = p => p > 3;

            arrayActual = new List<int>();
            actionTest(array, action, breakBeforeFunc, null);
            AssertCommon.AreEqual<IList>(new int[] { 1, 2, 3, 4 }, arrayActual);

            arrayActual = new List<int>();
            actionTest(array, action, null, breakAfterFunc);
            AssertCommon.AreEqual<IList>(new int[] { 1, 2, 3, 4 }, arrayActual);

            arrayActual = new List<int>();
            breakBeforeFunc = p => p > 3;
            actionTest(array, action, breakBeforeFunc, breakAfterFunc);
            AssertCommon.AreEqual<IList>(new int[] { 1, 2, 3 }, arrayActual);

            arrayActual = new List<int>();
            breakAfterFunc = p => p > 1;
            actionTest(array, action, breakBeforeFunc, breakAfterFunc);
            AssertCommon.AreEqual<IList>(new int[] { 1 }, arrayActual);
        }

        [TestMethod()]
        public void ForeachHandleTest()
        {
            ForeachHandleTestHelper(new Action<IEnumerable, Action<int>>(UtilityCommon.ForeachHandle<int>));
        }

        [TestMethod()]
        public void ForeachHandleGenericTest()
        {
            ForeachHandleTestHelper(new Action<IEnumerable<int>, Action<int>>(UtilityCommon.ForeachHandle<int>));
        }

        [TestMethod()]
        public void ForeachHandleHasBreakTest()
        {
            ForeachHandleTestHelper(new Action<IEnumerable, Action<int>, Func<int, bool>, Func<int, bool>>(
                UtilityCommon.ForeachHandle<int>), true, true);
        }

        [TestMethod()]
        public void ForeachHandleGenericHasBreakTest()
        {
            ForeachHandleTestHelper(new Action<IEnumerable<int>, Action<int>, Func<int, bool>, Func<int, bool>>(
                UtilityCommon.ForeachHandle<int>), true, true);
        }
    }
}
